package com.vcs.scheduler.listeners;

import java.text.ParseException;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

import com.vcs.scheduler.quartz.ResumeServicesMailJob;
import com.vcs.scheduler.quartz.ResumeServicesMailTask;

/**
 * Application Lifecycle Listener implementation class QuartzSchedulerListener
 * 
 */
public class QuartzSchedulerListener implements ServletContextListener {

	/**
	 * Default constructor.
	 */
	public QuartzSchedulerListener() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent arg0) {
		ResumeServicesMailTask task = new ResumeServicesMailTask();

		// specify your sceduler task details
		JobDetail job = new JobDetail();
		job.setName("jobName");
		job.setJobClass(ResumeServicesMailJob.class);

		Map dataMap = job.getJobDataMap();
		dataMap.put("resumeServicesMailTask", task);

		try {
			// configure the scheduler time, run it every 5 seconds
			CronTrigger trigger = new CronTrigger();
			trigger.setName("ResumeServicesMail");
			//trigger.setCronExpression("0/10 * * * * ?");
			
			// This will run every wednesday at 1 AM Midnight
			trigger.setCronExpression("0 0 1 ? * WED");

			// schedule it
			Scheduler scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.start();
			scheduler.scheduleJob(job, trigger);

		} catch (ParseException e) {
			e.printStackTrace();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
	}

}
